package extracteur;



import java.io.IOException;
import java.util.Arrays;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;





public class XmlParseur extends DefaultHandler {

	String xml_file;
	
	/*symbol of tag*/
	String tagObjectiveValue;
	String tagVariable;
	
	/*symbol of attribut*/
	String attributObjectiveValue;
	String attributVaribleValeur;
	
	/*value of attribut*/
	String valeurObjectiveValue;
	String valeurVaribleValeur;
	
	
	/*temporary variables */	
	private String tempVal;
	private Attributes tempAttributes;
	
	/*data*/
	String objectiveValue;
	String [][] tableVariables;
	
	/*nombre de variable x*/
	int nombreVariable;
	int compteur;
	
	public XmlParseur(String xml_file) {
	
		this.xml_file=xml_file;
		tagObjectiveValue="header";
		tagVariable="variable";
		attributObjectiveValue = "objectiveValue";
		attributVaribleValeur="value";
		valeurObjectiveValue="";
		valeurVaribleValeur="";
		tableVariables = new String[7][5];
		nombreVariable = 35;
		compteur=0;
		this.parseDocument();
	}
	
	public XmlParseur(String xml_file,String tagObjectiveValue,String tagVariable,String attributObjectiveValue,String attributValeurVarible) {
		this.xml_file=xml_file;
		this.tagObjectiveValue=tagObjectiveValue;
		this.tagVariable=tagVariable;
		this.attributObjectiveValue =attributObjectiveValue;
		this.attributVaribleValeur=attributValeurVarible;
		valeurObjectiveValue="";
		valeurVaribleValeur="";
		tableVariables = new String[7][5];
		nombreVariable = 35;
		compteur=0;
		
		this.parseDocument();
	}
	
	
	private void parseDocument() 
	{
		compteur=0;
		//get a factory
		SAXParserFactory spf = SAXParserFactory.newInstance();
		try {
		
			/*URL url = new URL(url_xml_file);
			InputStream input = url.openStream();*/
			spf.setNamespaceAware(false);

			//get a new instance of parser
			SAXParser sp = spf.newSAXParser();
			
			//parse the file and also register this class for call backs
			//sp.parse(input, this);
			sp.parse(xml_file, this);
			
		}catch(SAXException se) {
			se.printStackTrace();
		}catch(ParserConfigurationException pce) {
			pce.printStackTrace();
		}catch (IOException ie) {
			ie.printStackTrace();
		}
	}


	

	public String getTagObjectiveValue() {
		return tagObjectiveValue;
	}

	public void setTagObjectiveValue(String tagObjectiveValue) {
		this.tagObjectiveValue = tagObjectiveValue;
	}

	public String getTagVariable() {
		return tagVariable;
	}

	public void setTagVariable(String tagVariable) {
		this.tagVariable = tagVariable;
	}

	public String getAttributObjectiveValue() {
		return attributObjectiveValue;
	}

	public void setAttributObjectiveValue(String attributObjectiveValue) {
		this.attributObjectiveValue = attributObjectiveValue;
	}

	public String getAttributVaribleValeur() {
		return attributVaribleValeur;
	}

	public void setAttributVaribleValeur(String attributVaribleValeur) {
		this.attributVaribleValeur = attributVaribleValeur;
	}

	public String getValeurObjectiveValue() {
		return valeurObjectiveValue;
	}

	public void setValeurObjectiveValue(String valeurObjectiveValue) {
		this.valeurObjectiveValue = valeurObjectiveValue;
	}

	public String getValeurVaribleValeur() {
		return valeurVaribleValeur;
	}

	public void setValeurVaribleValeur(String valeurVaribleValeur) {
		this.valeurVaribleValeur = valeurVaribleValeur;
	}

	
	//Event Handlers
		public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException 
		{
			tempVal = "";
			this.tempAttributes = null;
			
			
			if(attributes.getLength() != 0)
			{
				this.tempAttributes = attributes;	
			}
			
		}
		
		public void characters(char[] ch, int start, int length) throws SAXException 
		{
			tempVal = new String(ch,start,length);
		}
		
		
		
		public void endElement(String uri, String localName, String qName) throws SAXException 
		{
			
			
		

			if(qName.equalsIgnoreCase(this.tagObjectiveValue)) 
			{
				if(this.tempAttributes != null)
				{
					
					
					this.valeurObjectiveValue = this.tempAttributes.getValue(attributObjectiveValue);
					
					this.objectiveValue = valeurObjectiveValue;
					
					
				}
			}
			else if(qName.equalsIgnoreCase(this.tagVariable))
			{
				
				if(this.tempAttributes != null)
				{
					
					if(this.compteur<this.nombreVariable)
					{
						this.valeurVaribleValeur = this.tempAttributes.getValue(this.attributVaribleValeur);
					
						//System.out.println(" "+(compteur/5+1)+" "+(compteur-(compteur/5)*5)+" [valeur] "+this.valeurVaribleValeur)
						
						this.tableVariables[(compteur/5)][(compteur-(compteur/5)*5)] =valeurVaribleValeur;
					}
				}
				this.compteur++;
				
			}
				
			//System.out.println("--------------------end qName "+qName);
			
		}
		
		
		
		
	@Override
		public String toString() {
			return "XmlParseur [objectiveValue=" + objectiveValue
					+ ", tableVariables=" + Arrays.toString(tableVariables)
					+ "]";
		}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		XmlParseur x= new XmlParseur("res.xml");
		String[][] tempTableVariables = x.getTableVariables();
		
		System.out.println("objectiveValue "+x.getObjectiveValue());
		for (int i = 0; i < 7; i++){ 

			for(int j = 0; j < 5; j++){ 

				System.out.println("valeur["+ i +"]["+ j+"]: "+tempTableVariables[i][j]);
			} 
			} 
		
		
		
	}

	public String getObjectiveValue() {
		return objectiveValue;
	}

	public void setObjectiveValue(String objectiveValue) {
		this.objectiveValue = objectiveValue;
	}

	public String[][] getTableVariables() {
		return tableVariables;
	}

	public void setTableVariables(String[][] tableVariables) {
		this.tableVariables = tableVariables;
	}
	
	
	
	
}
